<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :md="6" :sm="12">
            <a-form-item label="账号">
              <a-input placeholder="请输入账号查询" v-model="queryParam.username"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="12">
            <a-form-item label="服务名称">
              <a-input placeholder="请输入服务器名称" v-model="queryParam.virtualserve"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>

            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px">
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay" @click="handleMenuClick">
          <a-menu-item key="1">
            <a-icon type="delete" @click="batchDel"/>
            删除
          </a-menu-item>
          <a-menu-item key="2">
            <a-icon type="lock" @click="batchFrozen('2')"/>
            冻结
          </a-menu-item>
          <a-menu-item key="3">
            <a-icon type="unlock" @click="batchFrozen('1')"/>
            解冻
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>已选择&nbsp;<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.avatar)" icon="user"/>
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a id="apply"  @click="See()" v-show="record.status==1">
            <img class="src" src="../../img/peizhi.png" title="配置"></a>
          <a-divider type="vertical"/>

         <a @click="handleEdit(record)" v-show="record.status==1">
           <img class="src" src="../../img/qidong.png" title="启动"></a>
            <a-divider type="vertical"/>
          <a @click="statusEdit(record)"  v-show="record.status==1">
            <img class="src" src="../../img/tuihui.png" title="退回"></a>
          <a-divider type="vertical"/>

          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a><img class="src" src="../../img/shanchu.png" title="删除"></a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <uuid-modal ref="modalForm" @ok="modalFormOk"></uuid-modal>
    <back-modal ref="modalFormok" @ok="modalFormOk"></back-modal>
  </a-card>
</template>

<script>
/*  import UserModal from '../system/modules/UserModal'*/
  import UuidModal from './modules/UuidModal'
  import BackModal from './modules/BackModal'
  import {putAction} from '@/api/manage';
  import {frozenBatch} from '@/api/api'
  import {JantdListMixin} from '@/mixins/JantdListMixin'
  import {allocation} from '@/api/server'

  import { Builder, By, Key, until } from 'selenium-webdriver'
  import chromeDriverRouter from 'chromedriver'


  export default {
    name: "ServerList",
    mixins: [JantdListMixin],
    components: {
      UuidModal,
      BackModal
    },
    data() {
      return {
        description: '这是虚拟机服务管理页面',
/*        queryParam: {},*/
        queryParam:{username:'',virtualserve:''},
        allocation:'',
        columns: [
          /*{
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },*/


          {
            title: '用户名称',
            align: "center",
            dataIndex: 'username',
            width: 120
          },
          {
            title: '用户服务名称',
            align: "center",
            width: 100,
            dataIndex: 'virtualserve',
          },
          {
            title: '所在单位',
            align: "center",
            width: 80,
            dataIndex: 'departName',
          },
          {
            title: '服务状态',
            align: "center",
            width: 140,
            dataIndex: 'status',
            customRender:function (text) {
              if(text==1){
                return "申请审核中";
              }else if(text==0){
                return "申请退回";
              }else if(text==2){
                return "审核通过";
              }else{
                return text;
              }
            }
          },
          {
            title: '服务创建时间',
            align: "center",
            width: 140,
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: {customRender: 'action'},
            align: "center",
            width: 170
          }
        ],
        url: {
          imgerver: window._CONFIG['domianURL'] + "/sys/common/view",
          syncUser: "/process/extActProcess/doSyncUser",
          list: "/sys/myserver/list",
          delete: "/sys/myserver/delete",
          deleteBatch: "/sys/myserver/deleteBatch",

        }
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      async See() {
        var infoWindow = window.open('http://192.168.5.139:5000/');
        infoWindow.onload = function() {
          //infoWindow.document.getElementById("account-name").children("input").value = '2'
          //infoWindow.document.getElementById("tertcc").value="这里是你要赋的值ccccccc";
        }

        /*/!*  let webdriver = require('selenium-webdriver');*!/
        let driver = await new Builder().forBrowser('chrome').build();
        await driver.get('http://192.168.5.139:5000');
// driver.findElement({ className: 'login_btn' }).findElement({ tagName: 'a' }).click();

        await driver.wait(webdriver.until.titleIs("帐号登录")).then(() => {
         driver.sleep(1 * 1000).then(() => {
            /!*   let frameElement = webdriver.By.id('ptlogin_iframe');*!/
            driver.findElement(By.xpath("//div[@id=\"account-name\"]//input[@type=\"text\"]")).sendKeys("admin");
            driver.findElement(By.xpath("//div[@id=\"account-password\"]//input[@type=\"password\"]")).sendKeys("123456");
            /!*           let username = webdriver.By.xpath("//div[@id=\"account-name\"]//input[@type=\"text\"]");
                        let password = webdriver.By.xpath("//div[@id=\"account-password\"]//input[@type=\"password\"]");
                        username.sendKeys("admin");
                        password.sendKeys("123456");*!/
            /!*     WebElement login = webDriver.findElement(By.xpath("//div[@id=\"account-login-button\"]"));*!/
            driver.switchTo().frame(driver.findElement(frameElement)).then(() => {
              driver.findElement(By.xpath("//div[@id=\"account-login-button\"]")).click();
            });
          });

        });

        /!*      allocation().then((res) =>{
                  /!*     async: false,*!/
                console.log(res);
                if(res.success){
                  console.log(res);
                }else{
                  res;
                }
          /!*    window.location.href = "http://192.168.43.132:5000";*!/
           /!*   window.open('http://192.168.5.139:5000',"_blank")*!/
            })*!/*/
      },
      getAvatarView: function (avatar) {
        return this.url.imgerver + "/" + avatar;
      },

      batchFrozen: function (status) {
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return false;
        } else {
          let ids = "";
          let that = this;
          that.selectedRowKeys.forEach(function (val) {
            ids += val + ",";
          });
          that.$confirm({
            title: "确认操作",
            content: "是否" + (status == 1 ? "解冻" : "冻结") + "选中账号?",
            onOk: function () {
              frozenBatch({ids: ids, status: status}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              });
            }
          });
        }
      },
      handleMenuClick(e) {
        if (e.key == 1) {
          this.batchDel();
        } else if (e.key == 2) {
          this.batchFrozen(2);
        } else if (e.key == 3) {
          this.batchFrozen(1);
        }
      },
      handleFrozen: function (id, status) {
        let that = this;
        frozenBatch({ids: id, status: status}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.loadData();
          } else {
            that.$message.warning(res.message);
          }
        });
      }
    }

  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>